/**
 * 初始化dist
 * @param ele
 */
function initDist(ele) {
    $("#" + ele).summernote({
        placeholder: '请输入产品详细信息', table: 2, height: 280, lang: 'zh-CN',
        toolbar: [['style', ['style']],
            ['font', ['bold', 'italic', 'underline', 'strikethrough', 'superscript', 'subscript', 'clear']], ['fontname', ['fontname']],
            ['fontsize', ['fontsize']],
            ['color', ['color']],
            ['para', ['ul', 'ol', 'paragraph']],
            ['height', ['height']],
            ['table', ['table']],
            ['insert', ['link', 'picture', 'file', 'hr', 'video']],
            ['view', ['fullscreen', 'codeview']],
            ['help', ['help']],
        ], buttons: {"file": fileAdd},
        callbacks: {
            onMediaDelete: function (file, editor, $editable) {
                if (imgUrl == "") return;
                $.ajax({
                    url: '/Service/EasyUiService.ashx?Method=RemoveImg',
                    type: 'post',
                    dataType: 'json',
                    data: {'file': imgUrl},
                    success: function (d) {
                        console.log('移除成功!');
                    }
                })
            },
            onImageUpload: function (file, editor, $editable) {
                var size = file[0].size;
                if (size / 1024 / 1024 > 2) {
                    swal({text: "图片大小不能超过2M", button: '确认', icon: 'warning'});
                    return false;
                }
                var gs = file[0].name;
                if (gs != "") {
                    gs = gs.substring(gs.indexOf(".") + 1, gs.length), gs = gs.toUpperCase();
                }
                if (gs != "PNG" && gs != "TIFF" && gs != "BMP" && gs != "GIF" && gs != "PSD" && gs != "TGA" && gs != "EPS" && gs != "JPEG") {
                    swal({text: "格式不允许", button: '确认', icon: 'warning'});
                    return false;
                }
                var formData = new FormData();
                formData.append("file", file[0]);
                $.ajax({
                    url: '/api/file/upload',
                    type: 'post',
                    dataType: 'json',
                    contentType: false,
                    processData: false,
                    data: formData,
                    success: function (d) {
                        d.code == 200 ?
                            (imgUrl = "/api/file/get?filename=" + d.data, $("#summernote").summernote("insertImage", "/api/file/get?filename=" + d.data, "img")) :
                            (swal({text: "图片上传失败!", button: false, icon: "warning", timer: 2000}));
                    }
                })
            }
        }
    });
}

var fileAdd = function(content) {
    var ui = $.summernote.ui;
    var button = ui.button({
        contents: '<i class="fa fa-child" /><span class="glyphicon glyphicon-paperclip"></span>',
        tooltip: '上传文件',
        click: function () {$("#addFile_modal").modal("show");}
    })
    return button.render();
}
